// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   applyPay_check_ok.java

package draft.stock;

import include.nseer_cookie.*;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.*;

public class applyPay_check_ok extends HttpServlet
{

	public applyPay_check_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		HttpSession session = request.getSession();
		nseer_db_backup1 stock_db = new nseer_db_backup1(dbApplication);
		ValidataNumber validata = new ValidataNumber();
		counter count = new counter(dbApplication);
		ValidataTag vt = new ValidataTag();
		ValidataRecord vr = new ValidataRecord();
		try
		{
			if (stock_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String pay_ID = request.getParameter("pay_ID");
				String product_amount = request.getParameter("product_amount");
				String checker_ID = (String)session.getAttribute("human_IDD");
				String config_id = request.getParameter("config_id");
				int num = Integer.parseInt(product_amount);
				String payer_name = request.getParameter("payer_name");
				String payer_ID = request.getParameter("payer_ID");
				String reason = request.getParameter("reason");
				String not_return_tag = request.getParameter("not_return_tag");
				String register = request.getParameter("register");
				String register_time = request.getParameter("register_time");
				String demand_return_time = request.getParameter("demand_return_time");
				String check_time = request.getParameter("check_time");
				String checker = request.getParameter("checker");
				String bodyc = new String(request.getParameter("remark").getBytes("UTF-8"), "UTF-8");
				String remark = exchange.toHtml(bodyc);
				String product_IDn[] = request.getParameterValues("product_ID");
				String amountn[] = request.getParameterValues("amount");
				List rsList = GetWorkflow.getList(stock_db, "stock_config_workflow", "02");
				String elem[] = new String[3];
				String product_ID_group = ",";
				int p = 0;
				for (int i = 1; i <= num; i++)
				{
					String tem_amount = (new StringBuilder("amount")).append(i).toString();
					String amount = request.getParameter(tem_amount);
					if (amount.equals(""))
						amount = "0";
					if (!validata.validata(amount))
						p++;
				}

				for (int j = 1; j < product_IDn.length; j++)
				{
					product_ID_group = (new StringBuilder(String.valueOf(product_ID_group))).append(product_IDn[j]).append(",").toString();
					if (!validata.validata(amountn[j]))
						p++;
				}

				if (num == 0 && product_IDn.length == 1)
				{
					response.sendRedirect((new StringBuilder("draft/stock/applyPay_ok_c.jsp?pay_ID=")).append(pay_ID).toString());
				} else
				{
					int m = 0;
					for (int i = 1; i <= num; i++)
					{
						String tem_product_ID = (new StringBuilder("product_ID")).append(i).toString();
						String product_ID = request.getParameter(tem_product_ID);
						if (product_ID_group.indexOf(product_ID) != -1)
							m++;
					}

					if (vt.validata((String)dbSession.getAttribute("unit_db_name"), "stock_apply_pay", "pay_ID", pay_ID, "check_tag").equals("5") || vt.validata((String)dbSession.getAttribute("unit_db_name"), "stock_apply_pay", "pay_ID", pay_ID, "check_tag").equals("9"))
					{
						if (p == 0)
						{
							if (m == 0)
								try
								{
									String time = "";
									Date now = new Date();
									SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
									time = formatter.format(now);
									int n = 0;
									for (int i = 1; i <= num; i++)
									{
										String tem_available_amount = (new StringBuilder("available_amount")).append(i).toString();
										String tem_amount = (new StringBuilder("amount")).append(i).toString();
										String available_amount = request.getParameter(tem_available_amount);
										String amount = request.getParameter(tem_amount);
										if (Double.parseDouble(amount) > Double.parseDouble(available_amount))
											n++;
									}

									String stock_pay_ID = NseerId.getId("stock/pay", (String)dbSession.getAttribute("unit_db_name"));
									String gather_ID = NseerId.getId("stock/gather", (String)dbSession.getAttribute("unit_db_name"));
									double demand_amount = 0.0D;
									double cost_price_sum = 0.0D;
									for (int i = 1; i <= num; i++)
									{
										String tem_product_name = (new StringBuilder("product_name")).append(i).toString();
										String tem_product_ID = (new StringBuilder("product_ID")).append(i).toString();
										String tem_available_amount = (new StringBuilder("available_amount")).append(i).toString();
										String tem_amount = (new StringBuilder("amount")).append(i).toString();
										String tem_cost_price = (new StringBuilder("cost_price")).append(i).toString();
										String tem_amount_unit = (new StringBuilder("amount_unit")).append(i).toString();
										String product_name = request.getParameter(tem_product_name);
										String product_ID = request.getParameter(tem_product_ID);
										String available_amount = request.getParameter(tem_available_amount);
										String amount = request.getParameter(tem_amount);
										if (amount.equals(""))
											amount = "0";
										String cost_price2 = request.getParameter(tem_cost_price);
										StringTokenizer tokenTO3 = new StringTokenizer(cost_price2, ",");
										String cost_price;
										String cost_price1;
										for (cost_price = ""; tokenTO3.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
											cost_price1 = tokenTO3.nextToken();

										String amount_unit = request.getParameter(tem_amount_unit);
										double subtotal = Double.parseDouble(cost_price) * Double.parseDouble(amount);
										cost_price_sum += subtotal;
										demand_amount += Double.parseDouble(amount);
										String sql1 = (new StringBuilder("update stock_apply_pay_details set product_ID='")).append(product_ID).append("',product_name='").append(product_name).append("',amount='").append(amount).append("',cost_price='").append(cost_price).append("',subtotal='").append(subtotal).append("' where pay_ID='").append(pay_ID).append("' and details_number='").append(i).append("'").toString();
										stock_db.executeUpdate(sql1);
										if (rsList.size() == 0)
										{
											String sql2 = (new StringBuilder("insert into stock_pay_details(pay_ID,details_number,product_ID,product_name,cost_price,subtotal,amount,unpay_amount) values('")).append(stock_pay_ID).append("','").append(i).append("','").append(product_ID).append("','").append(product_name).append("','").append(cost_price).append("','").append(subtotal).append("','").append(amount).append("','").append(amount).append("')").toString();
											stock_db.executeUpdate(sql2);
											if (not_return_tag.equals("0"))
											{
												String sql3 = (new StringBuilder("insert into stock_gather_details(gather_ID,details_number,product_ID,product_name,cost_price,subtotal,amount,ungather_amount) values('")).append(gather_ID).append("','").append(i).append("','").append(product_ID).append("','").append(product_name).append("','").append(cost_price).append("','").append(subtotal).append("','").append(amount).append("','").append(amount).append("')").toString();
												stock_db.executeUpdate(sql3);
											}
										}
									}

									String cost_pricen[] = request.getParameterValues("cost_price");
									product_IDn = request.getParameterValues("product_ID");
									String product_namen[] = request.getParameterValues("product_name");
									String amount_unitn[] = request.getParameterValues("amount_unit");
									String product_describen[] = request.getParameterValues("product_describe");
									amountn = request.getParameterValues("amount");
									for (int i = 1; i < product_IDn.length; i++)
									{
										StringTokenizer tokenTO3 = new StringTokenizer(cost_pricen[i], ",");
										String cost_price;
										String cost_price1;
										for (cost_price = ""; tokenTO3.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
											cost_price1 = tokenTO3.nextToken();

										if (!amountn[i].equals("") && Double.parseDouble(amountn[i]) != 0.0D)
										{
											num++;
											double subtotal = Double.parseDouble(cost_price) * Double.parseDouble(amountn[i]);
											cost_price_sum += subtotal;
											demand_amount += Double.parseDouble(amountn[i]);
											String sql1 = (new StringBuilder("insert into stock_apply_pay_details(pay_ID,details_number,product_ID,product_name,product_describe,amount,amount_unit,cost_price,subtotal) values ('")).append(pay_ID).append("','").append(num).append("','").append(product_IDn[i]).append("','").append(product_namen[i]).append("','").append(product_describen[i]).append("','").append(amountn[i]).append("','").append(amount_unitn[i]).append("','").append(cost_price).append("','").append(subtotal).append("')").toString();
											stock_db.executeUpdate(sql1);
											if (rsList.size() == 0)
											{
												String sql2 = (new StringBuilder("insert into stock_pay_details(pay_ID,details_number,product_ID,product_name,cost_price,subtotal,amount,unpay_amount) values('")).append(stock_pay_ID).append("','").append(num).append("','").append(product_IDn[i]).append("','").append(product_namen[i]).append("','").append(cost_price).append("','").append(subtotal).append("','").append(amountn[i]).append("','").append(amountn[i]).append("')").toString();
												stock_db.executeUpdate(sql2);
												if (not_return_tag.equals("0"))
												{
													String sql3 = (new StringBuilder("insert into stock_gather_details(gather_ID,details_number,product_ID,product_name,cost_price,subtotal,amount,ungather_amount) values('")).append(gather_ID).append("','").append(num).append("','").append(product_IDn[i]).append("','").append(product_namen[i]).append("','").append(cost_price).append("','").append(subtotal).append("','").append(amountn[i]).append("','").append(amountn[i]).append("')").toString();
													stock_db.executeUpdate(sql3);
												}
											}
										}
									}

									if (rsList.size() == 0)
									{
										String sql = (new StringBuilder("update stock_apply_pay set reason='")).append(reason).append("',register='").append(register).append("',register_time='").append(register_time).append("',demand_return_time='").append(demand_return_time).append("',check_time='").append(check_time).append("',checker='").append(checker).append("',remark='").append(remark).append("',demand_amount='").append(demand_amount).append("',cost_price_sum='").append(cost_price_sum).append("',not_return_tag='").append(not_return_tag).append("',check_tag='1' where pay_ID='").append(pay_ID).append("'").toString();
										stock_db.executeUpdate(sql);
										if (!vr.validata((String)dbSession.getAttribute("unit_db_name"), "stock_pay", "reasonexact", pay_ID))
										{
											String sql4 = (new StringBuilder("insert into stock_pay(pay_ID,reason,reasonexact,reasonexact_details,demand_amount,cost_price_sum,register,register_time) values('")).append(stock_pay_ID).append("','").append(reason).append("','").append(pay_ID).append("','").append(payer_name).append("','").append(demand_amount).append("','").append(cost_price_sum).append("','").append(checker).append("','").append(check_time).append("')").toString();
											stock_db.executeUpdate(sql4);
										}
										if (not_return_tag.equals("0") && !vr.validata((String)dbSession.getAttribute("unit_db_name"), "stock_gather", "reasonexact", pay_ID))
										{
											String sql5 = (new StringBuilder("insert into stock_gather(gather_ID,reason,reasonexact,reasonexact_details,demand_amount,cost_price_sum,register,register_time) values('")).append(gather_ID).append("','").append(reason).append("','").append(pay_ID).append("','").append(payer_name).append("','").append(demand_amount).append("','").append(cost_price_sum).append("','").append(checker).append("','").append(check_time).append("')").toString();
											stock_db.executeUpdate(sql5);
										}
									} else
									{
										String sql = (new StringBuilder("update stock_apply_pay set reason='")).append(reason).append("',register='").append(register).append("',register_time='").append(register_time).append("',demand_return_time='").append(demand_return_time).append("',check_time='").append(check_time).append("',checker='").append(checker).append("',remark='").append(remark).append("',demand_amount='").append(demand_amount).append("',cost_price_sum='").append(cost_price_sum).append("',not_return_tag='").append(not_return_tag).append("',check_tag='0' where pay_ID='").append(pay_ID).append("'").toString();
										stock_db.executeUpdate(sql);
										String nseer_sql;
										for (Iterator ite = rsList.iterator(); ite.hasNext(); stock_db.executeUpdate(nseer_sql))
										{
											elem = (String[])ite.next();
											nseer_sql = (new StringBuilder("insert into stock_workflow(config_id,object_ID,describe1,describe2) values ('")).append(elem[0]).append("','").append(pay_ID).append("','").append(elem[1]).append("','").append(elem[2]).append("')").toString();
										}

									}
									response.sendRedirect("draft/stock/applyPay_ok.jsp?finished_tag=5");
								}
								catch (Exception ex)
								{
									ex.printStackTrace();
								}
							else
								response.sendRedirect("draft/stock/applyPay_ok.jsp?finished_tag=6");
						} else
						{
							response.sendRedirect("draft/stock/applyPay_ok.jsp?finished_tag=2");
						}
					} else
					{
						response.sendRedirect("draft/stock/applyPay_ok.jsp?finished_tag=7");
					}
				}
				stock_db.commit();
				stock_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
